# -*- coding: utf-8 -*-
'''
Author: your name
Date: 2021-02-01 15:20:42
LastEditTime: 2021-02-01 17:39:17
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /职级爬虫脚本/demo.py
'''
import requests
import json
from pprint import pprint


def get_data(url):
    r = requests.get(url)
    json_tmp = r.json()
    json_str = json.dumps(json_tmp)
    user_dic = json.loads(json_str)
    return user_dic


user_dic = get_data(
    "http://www.salaryfly.com/_next/data/iZlsg4aFK1pKASXk-o2Rt/index.json")
lst = user_dic["pageProps"]["companyCandidates"]


def str_url(c_id, type, level):
    return "https://api.salaryfly.com:8080/salary?id="+str(c_id)+"&type="+str(type)+"&level="+str(level)


work_list = {
    "1": "技术",
    "2": "产品经理",
    "3": "运营/增长",
    "4": "UX设计师"
}

for i in lst:
    for j in i["levels"]:
        for tpe in ["1", "2", "3", "4"]:
            pprint(i)
            if i == {} | j == {}:
                break
            var01 = str(i["name"])
            # var02 = str(i["pinyin"])
            var03 = str(i["stock_code"])
            var04 = str(i["tag"])
            var05 = str(i["url"])
            var1 = str(i["bit_logo"])
            var2 = str(i["desc"])
            var3 = str(i["id"])
            var4 = str(i["img_logo"])
            var5 = str(j["company_id"])
            var6 = str(j["desc"])
            var7 = str(j["gmt_created"])
            var8 = str(j["id"])
            var9 = str(j["name"])
            var10 = str(j["order"])
            var11 = str(j["percent"])
            var12 = str(j["state"])
            var13 = str(j["title"])
            t = get_data(str_url(var3, tpe, var9))["result"]
            if t == {}:
                break
            var14 = str(t["basic_avg"])
            var15 = str(t["bonus_avg"])
            var16 = str(t["satisfaction_avg"])
            var17 = str(t["stock_avg"])
            var18 = str(t["stock_median"])
            var19 = str(t["total_avg"])
            var20 = str(tpe)

            # c_name = i["name"]
            # c_id = str(i["id"])
            # name = str(j["name"])
            # info = "公司名称:"+c_name+"-公司ID:"+c_id + \
            #     "-种类:"+work_list[tpe]+"-职级名称:"+name
            # pprint(info)
            # url = str_url(c_id, tpe, name)
            # pprint(get_data(url))
            split_char = "@"
            f = open('data.txt', 'a+')  # 若是'wb'就表示写二进制文件
            f.writelines(var01+split_char +
                        #  var02+split_char +
                         var03+split_char +
                         var04+split_char +
                         var05+split_char +
                         var1+split_char +
                         var2+split_char +
                         var3+split_char +
                         var4+split_char +
                         var5+split_char +
                         var6+split_char +
                         var7+split_char +
                         var8+split_char +
                         var9+split_char +
                         var10+split_char +
                         var11+split_char +
                         var12+split_char +
                         var13+split_char +
                         var14+split_char +
                         var15+split_char +
                         var16+split_char +
                         var17+split_char +
                         var18+split_char +
                         var19+split_char +
                         var20+"\n")
            # f.writelines(info+"\n")
            # f.writelines(str(get_data(url)["result"])+"\n\n")
            f.close()
